//
//  NIAvatarView.m
//  HavalConnect
//
//  Created by NAVINFO on 2017/12/19.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIAvatarView.h"
@interface NIAvatarView()
{
    
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIImageView *avatarImageView;
    __weak IBOutlet UIImageView *loopImageView;
    __weak IBOutlet UIImageView *defultImageView;
    
    UIImage *image;
}

@end




@implementation NIAvatarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    contentView.backgroundColor = [UIColor whiteColor];
    [avatarImageView setNeedsLayout];
    [avatarImageView layoutIfNeeded];
    
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    avatarImageView.layer.masksToBounds = YES;
    avatarImageView.layer.cornerRadius = avatarImageView.frame.size.width/2;
}

-(void)setAvatarImage:(UIImage *)avatar
{
    
    image = avatar;
    [avatarImageView setImage:avatar];
    defultImageView.hidden = YES;
    avatarImageView.hidden = NO;
    
}

-(void)setDefultImage:(UIImage *)image
{
    [defultImageView setImage:image];
    defultImageView.hidden = NO;
    avatarImageView.hidden = YES;
}

-(void)hiddenLoop:(BOOL)hidden
{
    loopImageView.hidden = hidden;
}

@end
